﻿using System;
using System.Web;

namespace Sucdri.Infrastructure.Core.Context
{
   internal sealed class WebContextContainer : IContextContainer
   {
      #region IContextContainer 成员

      public void Add(string key, object value)
      {
         if (this[key] != null)
            throw new ArgumentException(string.Format("相同键值{0}的元素已经存在", key), "key");
         this[key] = value;
      }

      public bool Contains(string key)
      {
         return (this[key] != null);
      }

      public void Remove(string key)
      {
         this[key] = null;
      }

      public object this[string key]
      {
         get
         {
            Guard.ArgumentNotNull("key",key);
            return HttpContext.Current.Items[key];
         }
         set
         {
            Guard.ArgumentNotNull("key", key);
           
            if(value==null)
               HttpContext.Current.Items.Remove(key);
            HttpContext.Current.Items[key]=value;   
         }
      }

      #endregion
   }
}